//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import BScroll from "better-scroll";
export default {
  name:'',
  //import引入的组件需要注入到对象中才能使用
  components: {},
  
  data() {
  return {
    applyForm:{
      goods_id:'',
      student_id:'',
      mobile:'',
      work_name:'',
      work_url:'',
      teacher_name:'',
      teacher_mobile:'',
      group_name:'',
      code:'',
      unit:''
    },
    studentName:'',
    fileList:[],
    uploadUrl: "upload.uploadPic", //上传图片
    selectImgIndex:0,
    stopButton: false,
    iPnoneBottomBol: false, //是否是苹果手机
    sexShow:false,
    studentShow:false,
    radio:'1',
    applyTitle:'选择参赛学员',
    studentArr:[],
    studentIndex:0,
    groupIndex:0,
    couponScroll:"",
    relationName:'',
    relationArr:[],
    //上传文件
    fileType:'',
    //验证码
    codeText:"获取验证码",
    isDisable:false,
    timer:null,
    title:'',
    iPnoneBottomBol:''
  };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    getiPnoneBottom() { //检测是否为苹果手机
      var userAgent = navigator.userAgent;
      var isiOS = !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
      isiOS?this.iPnoneBottomBol = true:this.iPnoneBottomBol = false;
    },
    studentTo() {
      this.$router.push(this.fun.getUrl("actionStudent", {}, {}));
    },
    timeReduce() {
      // 定时器方法
      let _this = this
      let time = 60
      this.timer = setInterval(() => {
        this.codeText = `重新发送${time}s`
        time -= 1
        this.isDisable = true
        if (time <= 0) {
          this.isDisable = false
          this.codeText = '获取验证码'
          clearInterval(_this.timer)
        }
      }, 500)
    },
    verificationOn() {
      if (this.fun.isTextEmpty(this.applyForm.mobile)) {
        Toast("请填写手机号");
        return;
      }
      $http.post("plugin.event-registration.api.goods.goods.send-mobile-code", {mobile:this.applyForm.mobile}, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
           console.log(res);
           this.timeReduce();
          } else {
            
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    initData() {
      this.applyForm.student_id = '';
      this.applyForm.mobile ='';
      this.studentName ='';
      this.applyForm.work_name ='';
      this.applyForm.work_url ='';
      this.applyForm.teacher_name ='';
      this.applyForm.teacher_mobile ='';
      this.applyForm.group_name ='';
      this.applyForm.code ='';
      this.applyForm.unit ='';
      this.relationName = ''
    },
    applyAffirm() {
      if (this.fun.isTextEmpty(this.applyForm.student_id)) {
        this.$toast("请选择参赛学员");
        return;
      }
      if (this.fun.isTextEmpty(this.applyForm.mobile)) {
        this.$toast("请填写手机号码");
        return;
      }
      if (this.fun.isTextEmpty(this.applyForm.code)) {
        this.$toast("请填写手机验证码");
        return;
      }
      if (this.fun.isTextEmpty(this.applyForm.work_name)) {
        this.$toast("请填写参赛作品名称");
        return;
      }
      if (this.fun.isTextEmpty(this.applyForm.group_name)) {
        this.$toast("请选择参赛组别");
        return;
      }
      if (this.fun.isTextEmpty(this.applyForm.work_url)) {
        this.$toast("请上传作品");
        return;
      }
      $http.post("plugin.event-registration.api.goods.goods.entry", this.applyForm, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.$router.push(
              this.fun.getUrl(
                "goodsorder",
                {},
                {
                  goodsId: this.applyForm.goods_id,
                  optionsId:0,
                  total: 1,
                  applyId:res.apply_id,
                  tag: "apply"
                }
              )
            );
          } else {
            
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    async onSearch() {
      this.studentArr = [];
      let res  =  await $http.get("plugin.event-registration.api.goods.goods.get-student-list", {relation_name:this.relationName}, "正在获取");
      console.log(res);
      if(res.result) {
        this.studentArr = res.data || [];
        this.relationArr = res.data || [];
      } else {
        this.$toast(res.msg)
      }
    },
    studentOn(item,index) {
      if(this.applyType == 'student') {
        this.applyForm.student_id = item.id;
        this.studentName = item.relation_name;
        this.studentIndex = index;
      } else {
        this.applyForm.group_name = item.group_name;
        this.groupIndex = index;
      }
      
      
      
      this.studentShow = false;
    },
    studentClose() {
      this.studentShow = false;
    },
    studentList() {
      $http.get("plugin.event-registration.api.goods.goods.get-student-list", {relation_name:this.relationName}, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
           console.log(res);
            this.relationArr = res;
            
          } else {
            
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    groupData() {
      $http.get("plugin.event-registration.api.goods.goods.get-group", {id:this.applyForm.goods_id}, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
           console.log(res);
            this.groupArr = res;
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    studentPopOn(name) {
      this.applyType = name;
      
      if(name == 'student') {
        this.applyTitle = '选择参赛学员';
        this.studentArr = this.relationArr;
      } else {
        this.applyTitle = '选择参赛组别';
        this.studentArr = this.groupArr; 
      }
      this.studentShow = true;
    },
    sexPopColse() {
      this.sexShow = false;
    },
    selectIndex(i) {
      if (this.stopButton) {
        return false;
      }
      this.selectImgIndex = i;
    },
     /*vant图片上传*/
     onRead(e, flag) {
      console.log(e);
      this.fileType = e.file.type;
      this.$toast.loading({
        message: "上传中",
        forbidClick: true
      });
      this.stopButton = true;
      let fd = new FormData();

      if (flag === true) {
        fd.append("file", e); // 第一个参数字符串可以填任意命名，第二个根据对象属性来找到file
      } else {
        fd.append("file", e.file); // 第一个参数字符串可以填任意命名，第二个根据对象属性来找到file
      }
      var That = this;
      axios
        .post(this.uploadUrl, fd, {
          headers: { "Content-Type": "multipart/form-data" }
        })
        .then(response => {
          var responseData = response.data;
          if (responseData.result === 1) {
            this.$toast.clear();
            this.applyForm.work_url = responseData.data.img_url;

          } else {
            // Toast(responseData.msg);
            this.$toast(responseData.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
  
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
  
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    this.uploadUrl = this.fun.getRealUrl("upload.uploadPic", {});
    this.studentShow = false;
    this.initData();
    if (this.$route.query.goodsId) {
      this.applyForm.goods_id = Number(this.$route.query.goodsId);
      // this.goodsDetail();
      this.title = this.$route.query.title;
      this.groupData();
    }
    this.getiPnoneBottom();
    this.studentList();
  }, //如果页面有keep-alive缓存功能，这个函数会触发
  }